package com.lu.widgets.component.ime;

import java.util.ArrayList;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

public abstract class IMEWithoutMask extends RelativeLayout implements
		IIMEAnimation {
	protected static int contentViewRes = 0;
	protected EditText editText;
	protected Button buttonChange;
	protected Button buttonDelete;
	protected ArrayList<Button> buttonChars = new ArrayList<Button>();

	public IMEWithoutMask(Context context, AttributeSet attrs, int defStyle,
			int contentViewRes) {
		super(context, attrs, defStyle);
		LayoutInflater.from(context).inflate(contentViewRes, this, true);
		if (!isInEditMode()) {
			findAllView();
			setListener();
			setDefaultProperties();
		}
	}

	public IMEWithoutMask(Context context, AttributeSet attrs, int defStyle) {
		this(context, attrs, defStyle, contentViewRes);
	}

	public IMEWithoutMask(Context context, AttributeSet attrs) {
		this(context, attrs, 0, contentViewRes);
	}

	public IMEWithoutMask(Context context) {
		this(context, null, 0, contentViewRes);
	}

	protected abstract void findAllView();

	protected void setDefaultProperties() {
		setVisibility(INVISIBLE);
	}

	protected void setListener() {
		setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
			}
		});
		setOnClickListener(EnumIMEButton.Change, null);
		setOnClickListener(EnumIMEButton.Delete, null);
		setOnClickListener(EnumIMEButton.Chars, null);
	}

	public void enableChangeBtn(boolean enabled) {
		buttonChange.setEnabled(enabled);
	}

	public void setTag(EditText editText) {
		super.setTag(editText);
		this.editText = editText;
		this.editText.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				display();
				return true;
			}
		});
	}

	public void setOnClickListener(EnumIMEButton btn, final OnClickListener l) {
		switch (btn) {
		case Change:
			buttonChange.setOnClickListener(l);
			break;
		case Delete:
			if (buttonDelete != null)
				buttonDelete.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						onDeleteClick(v);
						if (l != null)
							l.onClick(v);
					}
				});
			break;
		case Chars:
			OnClickListener cl = new OnClickListener() {
				@Override
				public void onClick(View v) {
					onCharClick(v);
					if (l != null)
						l.onClick(v);
				}
			};
			for (Button b : buttonChars)
				b.setOnClickListener(cl);
			break;
		default:
			break;
		}
	}

	public void onDeleteClick(View v) {
		if (editText == null)
			return;
		String str = editText.getText().toString();
		if (str.length() > 0)
			if (Math.abs(editText.getSelectionEnd()
					- editText.getSelectionStart()) > 0)
				editText.setText(str.substring(0, editText.getSelectionStart())
						+ str.substring(editText.getSelectionEnd()));
			else
				editText.setText(str.substring(0, str.length() - 1));
		editText.setSelection(editText.getText().length());
	}

	public void onCharClick(View v) {
		if (editText == null)
			return;
		String s = ((Button) v).getText().toString();
		String str = editText.getText().toString();
		if (Math.abs(editText.getSelectionEnd() - editText.getSelectionStart()) > 0)
			editText.setText(str.substring(0, editText.getSelectionStart()) + s
					+ str.substring(editText.getSelectionEnd()));
		else
			editText.setText(str.substring(0, editText.getSelectionEnd()) + s
					+ str.substring(editText.getSelectionEnd()));
		editText.setSelection(editText.getText().length());
	}

	@Override
	public boolean display() {
		setVisibility(VISIBLE);
		return true;
	}

	@Override
	public boolean change() {
		return false;
	}

	@Override
	public boolean hide() {
		setVisibility(INVISIBLE);
		return true;
	}
}
